import { create } from 'zustand'
import { UserType } from '@/api/user/types'
import { UserApi } from '@/api/user'

export interface UserState {
  user: UserType | null
  token: string | null
  // getUser: () => void
  setToken: (token: string) => void
  setUser: (user: UserType) => void
  clearUser: () => void
}

export const useUserStore = create<UserState>((set) => ({
  user: null,
  token: null,
  // login: (token: string) => {
  //   localStorage.setItem('authToken', token);
  // },
  setToken: (token: string) => {
    set({ token });
    localStorage.setItem('authToken', token)
  },
  // getUser: async () => {
  //   try {
  //     console.log('234')
  //     UserApi.getProfile().then(res => {
  //       console.log(res)
  //     })
  //     // console.log(res.data.user)
  //     // set({ user: res.data.user })
  //     // return res.data
  //   } catch (error) {

  //   }
  // },
  setUser: (user: UserType) => set({ user }),
  clearUser: () => set({ user: null }),
}))